package com.ghy.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.ghy.dto.ArticleInsertDTO;
import com.ghy.dto.ArticlePageDTO;
import com.ghy.dto.ArticleUpdateDTO;
import com.ghy.entity.Article;
import com.ghy.exception.ServiceException;
import com.ghy.vo.ArticleSimpleListVO;
import com.mybatisflex.core.query.QueryChain;
import com.mybatisflex.core.relation.RelationManager;
import com.mybatisflex.core.update.UpdateChain;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.ghy.entity.Article;
import com.ghy.mapper.ArticleMapper;
import com.ghy.service.ArticleService;
import org.springframework.stereotype.Service;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.Collection;
import java.util.List;

import static com.ghy.entity.table.ArticleTableDef.ARTICLE;
import static com.ghy.entity.table.CategoryTableDef.CATEGORY;
import static com.ghy.entity.table.MenuTableDef.MENU;


/**
 * 新闻表 服务层实现。
 *
 * @author ghy
 * @since 2024-12-22
 */
@Service
@CacheConfig(cacheNames = "article")
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article>  implements ArticleService{


    @CacheEvict(allEntries = true)
    @Override
    public boolean insert(ArticleInsertDTO dto) {

        // 标题查重
        if (QueryChain.of(mapper)
                .where(ARTICLE.TITLE.eq(dto.getTitle()))
                .exists()){
            throw new ServiceException("标题已存在");
        }

        //组装entity 实体类
        Article article = BeanUtil.copyProperties(dto, Article.class);

        article.setCreated(LocalDateTime.now());
        article.setUpdated(LocalDateTime.now());

        return mapper.insert(article) > 0;
    }

    @Cacheable(key = "#p0",
            condition = "#p0 != null",
            unless = "#result == null")
    @Override
    public Article select(Long id) {
        RelationManager.addQueryRelations("episode");
        return mapper.selectOneWithRelationsById(id);
    }

    @CacheEvict(allEntries = true)
    @Cacheable(key = "#root.methodName", unless = "#result == null")
    @Override
    public List<Article> top(Long n) {
        return QueryChain.of(mapper)
                .orderBy(ARTICLE.IDX.asc(), ARTICLE.ID.desc())
                .limit(n)
                .withRelations()
                .list();
    }

    @Cacheable(key = "#root.methodName",
            unless = "#result == null")
    @Override
    public List<ArticleSimpleListVO> simpleList() {
        return QueryChain.of(mapper)
                .withRelations ()
                .listAs(ArticleSimpleListVO.class);
    }

    @Cacheable(key ="#root.methodName + ':' + #p0.toString()",
            condition = "#p0 != null",
            unless = "#result == null")
    @Override
    public Page<Article> page(ArticlePageDTO dto) {
        QueryChain<Article> queryChain = QueryChain.of(mapper);


        return queryChain.withRelations()
                .page(new Page<>(dto.getPageNum(),dto.getPageSize()));
    }

    @CacheEvict(allEntries = true)
    @Override
    public boolean update(ArticleUpdateDTO dto) {
        //组装entity实体类
        Article article = BeanUtil.copyProperties(dto, Article.class);

        article.setUpdated(LocalDateTime.now());

        return UpdateChain.of(article)
                .where(ARTICLE.ID.eq(article.getId()))
                .update();
    }

    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(allEntries = true)
    @Override
    public boolean delete(Long id) {

        //删除父评论
        return mapper.deleteById(id) > 0;

    }

    @Transactional(rollbackFor = Exception.class)
    @CacheEvict(allEntries = true)
    @Override
    public boolean deleteBatch(List<Long> ids) {

        //删除父评论
        return mapper.deleteBatchByIds(ids) > 0;
    }

    

}
